package Hot100.DP;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class wordBreak {
    public static void main(String[] args) {
        String s = "apple";
        String[] wordDict = {"app", "le","appl"};
        List<String> list = new ArrayList<>();
        for (int i = 0; i < wordDict.length; i++) {
            list.add(wordDict[i]);
        }
        System.out.println(new wordBreak().wordBreak(s,list));
    }
    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> set = new HashSet<>(wordDict);
        //dp[j]数组表示 数组长度为j时能否被装满
        boolean dp[]  = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length() ; i++) {
            for (int j = 0; j < i && !dp[i]; j++) {
                if(set.contains(s.substring(j,i)) && dp[j]){
                    dp[i] = true;
                }
            }
        }
        return dp[s.length()];
    }
}
